import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/widgets/chart/kq_chart.dart';
import 'package:kq_flutter_core_widget/widgets/chart/pie/pie_chart.dart';
import 'package:get/get.dart';

/// 饼状图
class PieChartPage extends StatelessWidget {
  const PieChartPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('饼状图'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: Get.back,
        ),
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart2(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildPieChart3(),
            ),
          ),
          const SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: Text(
                "下面为两种无数据的展示方式：",
                style: TextStyle(fontSize: 20),
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar(),
            ),
          ),
          SliverToBoxAdapter(
            child: Align(
              alignment: Alignment.center,
              child: buildEmptyPieChar2(),
            ),
          )
        ],
      ),
    );
  }

  /// 常规饼状图
  Widget buildPieChart() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChartDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90,
          dataRender: PieChartDataRender(
              highLightRender: PieChartHighLightRender(
                  offsetRadius: 0,
                  colorBuilder: (p0) {
                    return HSLColor.fromColor(p0.color)
                        .withLightness(0.7)
                        .toColor();
                  })),
          labelRender: const PieChartLabelRender(),
          gestureHandler: PieChartGestureHandler(
            dragEnable: false
          ),
          data: [
            PieChartEntity(
                color: Colors.redAccent,
                value: 10,
                drawLabel: true,
                label: "测试",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试2",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.blueAccent,
                value: 33.5,
                drawLabel: true,
                label: "测试1",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试12",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.brown,
                value: 10,
                drawLabel: true,
                label: "测试2",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试22",
                secondLabelSize: 8),
            PieChartEntity(
                color: Colors.lightGreen,
                value: 19.3,
                drawLabel: true,
                label: "测试3",
                labelSize: 10,
                labelColor: Colors.white,
                secondLabel: "测试32",
                secondLabelSize: 8)
          ],
          labelPosition: LabelPosition.inside(),
          sliceSpace: 0,
          innerRadius: 0,
        ),
      ),
    );
  }

  /// 圆环饼状图
  Widget buildPieChart2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChartDelegate.withDefault(
            animDuration: const Duration(seconds: 2),
            radius: 90,
            gestureHandler: PieChartGestureHandler(),
            data: [
              PieChartEntity(
                  color: Colors.redAccent,
                  value: 11,
                  drawLabel: true,
                  label: "测试",
                  labelSize: 10,
                  labelColor: Colors.redAccent,
                  secondLabel: "测试2",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.blueAccent,
                  value: 33.5,
                  drawLabel: true,
                  label: "测试1",
                  labelSize: 10,
                  labelColor: Colors.blueAccent,
                  secondLabel: "测试12",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.brown,
                  value: 10,
                  drawLabel: true,
                  label: "测试2",
                  labelSize: 10,
                  labelColor: Colors.brown,
                  secondLabel: "测试22",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.lightGreen,
                  value: 19.3,
                  drawLabel: true,
                  label: "测试3",
                  labelSize: 10,
                  labelColor: Colors.lightGreen,
                  secondLabel: "测试32",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.teal,
                  value: 19.3,
                  drawLabel: true,
                  label: "测试4",
                  labelSize: 10,
                  labelColor: Colors.teal,
                  secondLabel: "测试42",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.blueGrey,
                  value: 19.3,
                  drawLabel: true,
                  label: "测试5",
                  labelSize: 10,
                  labelColor: Colors.blueGrey,
                  secondLabel: "测试52",
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.deepPurple,
                  value: 3,
                  drawLabel: true,
                  label: "测试6",
                  labelSize: 10,
                  labelColor: Colors.deepPurple,
                  secondLabel: '测试62',
                  secondLabelSize: 8),
              PieChartEntity(
                  color: Colors.purpleAccent,
                  value: 5,
                  drawLabel: true,
                  label: "测试7",
                  labelSize: 10,
                  labelColor: Colors.purpleAccent,
                  secondLabel: '测试72',
                  secondLabelSize: 8)
            ],
            labelPosition: LabelPosition.outside(
              lineFactory: (entity) {
                return IndicatorLine(
                    color: entity.color,
                    drawStartCircle: true,
                    circleColor: entity.color,
                    offset: 4);
              },
            ),
            dataRender: const PieChartDataRender(
                highLightRender: PieChartHighLightRender()),
            sliceSpace: 4,
            innerRadius: 60,
            labelRender: const PieChartLabelRender(
                minShowLabelAngle: 10, minShowLabelAngleByNear: 20)),
      ),
    );
  }

  /// 丁达尔玫瑰图
  Widget buildPieChart3() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChartDelegate(
          animDuration: const Duration(seconds: 2),
          radius: 90,
          dataRender: const PieChartDataRender(
              highLightRender:
                  PieChartHighLightRender(addedRadius: 20, offsetRadius: 0)),
          gestureHandler: PieChartGestureHandler(),
          data: [
            PieChartEntity(color: Colors.redAccent, value: 10),
            PieChartEntity(color: Colors.brown, value: 15, addedRadius: 10),
            PieChartEntity(
                color: Colors.lightGreen, value: 20, addedRadius: 20),
            PieChartEntity(
                color: Colors.blueAccent, value: 25, addedRadius: 30),
          ],
          sliceSpace: 0,
          innerRadius: 60,
        ),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar() {
    return Container(
      width: 160,
      height: 160,
      color: Colors.lightBlue[200],
      child: KqChart(
        delegate: KqPieChartDelegate.withDefault(radius: 70),
      ),
    );
  }

  /// 空数据饼状图
  Widget buildEmptyPieChar2() {
    return SizedBox(
      width: 260,
      height: 260,
      child: KqChart(
        delegate: KqPieChartDelegate(
          radius: 90,
          innerRadius: 60,
          dataRender: const PieChartDataRender(),
          baseMapColor: Colors.grey,
        ),
      ),
    );
  }
}
